#include <iostream>
using namespace std;
class Singleton {
    friend class AutoRelease;
public:
    static Singleton *getInstance(){
        if(pInstance == NULL){
            pInstance = new Singleton();
        }
        return pInstance;
    }
    static void destroy(){
        if(pInstance != NULL){
            delete  pInstance;
            pInstance = NULL;
        }
    }
private:
    Singleton(){}
    ~Singleton(){}
    static Singleton* pInstance;
};
//这个类托管了Singleton的指针，通过这个指针的生命周期来控制
//单例的结束
class AutoRelease
{
public:
    AutoRelease(Singleton *s)
        :_s(s)
    {
        cout<<"AutoRelease(Singleton)构造"<<endl;
    }
    ~AutoRelease(){
        cout<<"~AutoRelease()"<<endl;
        if(_s){
            delete _s;
            _s = nullptr;
        }
    }

private:
        Singleton *_s;

};
 //静态变量初始化
 Singleton *Singleton::pInstance = NULL;
void test(){
    AutoRelease ar(Singleton::getInstance());
}
int main()
{
    test();
    return 0;
}

